package com.briup.estore.common.service.impl;

import com.briup.estore.common.bean.*;
import com.briup.estore.common.dao.IOrderDao;
import com.briup.estore.common.dao.IOrderItemDao;
import com.briup.estore.common.dao.IShopCarDao;
import com.briup.estore.common.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.transaction.Transactional;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author adam
 * @date 2022/10/18
 */

@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private IOrderDao orderDao;
    @Autowired
    private IShopCarDao shopCarDao;

    @Autowired
    private IOrderItemDao itemDao;

    @Override
    public long countOrder() {
        long count = orderDao.count();
        return count;
    }

    @Override
    public Page<Order> findOrders(String orderId, String status, int pageSize, int pageNum) throws Exception {
        PageRequest of = PageRequest.of(pageNum - 1, pageSize);
        Page<Order> page = orderDao.findByIdContainsAndStatusContainsOrderByCreateDateDesc(orderId, status, of);

        if (page.isEmpty()) {
            throw new Exception("暂无数据");
        }
        return page;
    }

    @Override
    @Transactional
    public void updateOrder(Order order) throws Exception {
        if (!StringUtils.hasText(order.getId()) || !StringUtils.hasText(order.getStatus())) {
            throw new Exception("参数缺失");
        }
        if (StringUtils.hasText(order.getDistributionMode()) && StringUtils.hasText(order.getTrackingNumber())) {
            orderDao.updateOrder(order.getId(), order.getStatus(), order.getDistributionMode(), order.getTrackingNumber());
        } else {
            orderDao.updateOrder(order.getId(), order.getStatus());
        }


    }

    @Override
    public Order findOrderById(String id) throws Exception {
        Order order = orderDao.findById(id).orElseGet(null);
        if (ObjectUtils.isEmpty(order)) {
            throw new Exception("暂无数据");
        }
        return order;
    }


    @Override
    public List<Order> findUserAllOrders(Long userId) {
        List<Order> list = orderDao.findByUserIdOrderByCreateDate(userId);
        return list;
    }

    @Override
    @Transactional
    public Order saveOrder(Long[] shopCarIds, User user, Long addressId) {
        ShippingAddress address = new ShippingAddress(addressId);
        List<Long> asList = Arrays.asList(shopCarIds);
        List<ShopCar> list = shopCarDao.findAllById(asList);
        Order order = new Order(user, address);

        List<OrderItem> items = list.stream().map(i -> {
            OrderItem item = new OrderItem(i);
            item.setOrder(order);
            return item;
        }).collect(Collectors.toList());
        order.setOrderItems(items);
        order.setStatus("1");
        orderDao.save(order);
        itemDao.saveAll(items);
        shopCarDao.deleteAllById(asList);
        return order;

    }

    @Override
    public Order paySuccess(String orderId) {
        Order order = orderDao.getById(orderId);
        order.setStatus("2");
        orderDao.save(order);
        return order;
    }


}
